﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint
{
    public class SlideEvents
    {
        Dictionary<int, ObservableShape> mShapeObservers = new Dictionary<int, ObservableShape>();
        
        public delegate void ShapeChangedEventHandler(object sender, ShapeChangedEventArgs e);
        public delegate void SelectionChangedWithinShapeEventHandler(object sender, TextSelectionChangedWithinShapeEventArgs e);

        public event ShapeChangedEventHandler ShapeAdded;
        public event ShapeChangedEventHandler ShapeDeselected;
        public event ShapeChangedEventHandler ShapeDeleted;
        public event ShapeChangedEventHandler ShapeSelected;
        public event SelectionChangedWithinShapeEventHandler TextSelectionChanged;
        public event Ppt.EApplication_WindowSelectionChangeEventHandler SelectionChanged;

        public void RegisterObserver(ObservableShape o)
        {
            int shapeId = o.Shape.Id;
            if (mShapeObservers.ContainsKey(shapeId))
                mShapeObservers.Remove(shapeId);

            mShapeObservers.Add(o.Shape.Id, o);
        }

        public void RemoveObserver(ObservableShape o)
        {
            int shapeId = ((ResolvableShape)o.CachedShape.StoredObject).Id;
            mShapeObservers.Remove(shapeId);
        }

        public void OnSelectionChanged(Ppt.Selection sel)
        {
            if (SelectionChanged != null)
                SelectionChanged(sel);

            if (sel.ShapeRange.Count == 1)
            {
                int shapeId = sel.ShapeRange[1].Id;
                if (mShapeObservers.ContainsKey(shapeId))
                    mShapeObservers[shapeId].OnSelectedExclusively();
            }
        }
        
        public void OnShapeAdded(ShapeChangedEventArgs e)
        {
            if (ShapeAdded != null)
                ShapeAdded(this, e);
        }

        public void OnShapeDeleted(ShapeChangedEventArgs e)
        {
            if (ShapeDeleted != null)
                ShapeDeleted(this, e);

            if (mShapeObservers.ContainsKey(e.ShapeInfo.Id))
                mShapeObservers[e.ShapeInfo.Id].OnDeleted();
        }

        public void OnShapeSelected(ShapeChangedEventArgs e)
        {
            if (ShapeSelected != null)
                ShapeSelected(this, e);

            if (mShapeObservers.ContainsKey(e.ShapeInfo.Id))
                mShapeObservers[e.ShapeInfo.Id].OnSelected();
        }

        public void OnShapeDeselected(ShapeChangedEventArgs e)
        {
            if (ShapeDeselected != null)
                ShapeDeselected(this, e);

            if (mShapeObservers.ContainsKey(e.ShapeInfo.Id))
                mShapeObservers[e.ShapeInfo.Id].OnDeselected();
        }

        public void OnTextSelectionChanged(TextSelectionChangedWithinShapeEventArgs e)
        {
            if (TextSelectionChanged != null)
                TextSelectionChanged(this, e);

            int shapeId = e.TextRange.GetParent<Ppt.Shape>().Id;
            if (mShapeObservers.ContainsKey(shapeId))
                mShapeObservers[shapeId].OnTextSelectionChangedWithinShape();
        }
    }
}
